package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.IMessageDataDAL;
import com.qire.manhua.dal.IPageDataDAL;
import com.qire.manhua.model.enumeration.MessageType;
import com.qire.manhua.model.viewModel.impl.MessageCenterViewModel;
import com.qire.manhua.model.vo.MessageCenterPageVO;
import com.qire.manhua.model.vo.MessageCenterPageVO.NoticeVO;
import com.qire.manhua.model.vo.MessageCenterPageVO.PlatformMessageVO;
import com.qire.manhua.model.vo.MessageReadVO;

public class MessageCenterService extends DataService {

    private final IPageDataDAL pageDataDAL = WarehouseFactory.create(this, IPageDataDAL.class);
    private final IMessageDataDAL messageDataDAL = WarehouseFactory.create(this, IMessageDataDAL.class);

    private final MessageCenterViewModel viewModel;

    public MessageCenterService(MessageCenterViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 加载消息中心页面数据
     * @param pageNo 列表页号
     */
    public void loadPageData(int pageNo) {
        pageDataDAL.fetchMessageCenterPageData(pageNo);
    }
    @StrategyAnnotation(event = Url.MessageCenterPageData)
    public void fetchMessageCenterPageDataCallback(MessageCenterPageVO dataVO, boolean isCache) {

        viewModel.refreshLayoutFinish(300);

        if(ObjectUtil.isNull(dataVO)) {
            return;
        }

        NoticeVO commentVO = dataVO.post;
        if(ObjectUtil.nonNull(commentVO)) {
            viewModel.setCommentLastInfo(commentVO.new_cnt, commentVO.notice_title, commentVO.create_time);
        }
        NoticeVO likeVO = dataVO.like;
        if(ObjectUtil.nonNull(likeVO)) {
            viewModel.setLikeLastInfo(likeVO.new_cnt, likeVO.notice_title, likeVO.create_time);
        }

        if(ArrayUtil.isEmpty(dataVO.messages)) {
            viewModel.noMoreDataWasShown();
            return;
        }

        for(PlatformMessageVO messageVO : dataVO.messages) {

            int id = messageVO.id;
            String messageTitle = messageVO.message.msg_title;
            String messageContent = messageVO.message.msg_short_title;
            String messageCover = messageVO.message.msg_pic;
            String messageLink = messageVO.message.msg_url;
            String messageDate = messageVO.create_time;
            MessageType messageType = MessageType.build(messageVO.msg_type);
            boolean haveRead = messageVO.msg_state == 0;

            viewModel.addMessageItem(id, messageTitle, messageContent, messageCover, messageLink, messageDate, messageType, haveRead);
        }
        viewModel.refreshUI();

        if (!isCache) {
            viewModel.nextPage();
        }

    }

    /**
     * 标记某个消息已读
     * @param messageId 消息ID
     */
    public void markRead(int messageId) {
        messageDataDAL.readMessageByID(messageId);
    }
    /**
     * 标记全部已读
     */
    public void markAllRead() {
        messageDataDAL.readMessageOnAll();
    }
    @StrategyAnnotation(event = {Url.MessageReadOnSingle, Url.MessageReadOnAll})
    public void readMessageApiCallBack(MessageReadVO dataVO, boolean isCache) {
        // 在提前做了，没什么可做的。
        if("全部已读成功".equals(dataVO.ok)) {

        }
    }


}
